package com.maxmlr.music.system.controller;

import com.maxmlr.music.common.base.BaseController;
import com.maxmlr.music.common.bean.ResultBean;
import com.maxmlr.music.common.enums.ErrorCode;
import com.maxmlr.music.system.entity.Files;
import com.maxmlr.music.system.service.FilesService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author max
 * @version 1.0
 * @ClassName FilesController
 * @Description 文件上传 控制器
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-14
 */
@Slf4j
@RestController
@Api(value = "/system/files", tags = "Files 控制器")
@RequestMapping("/system/files")
public class FilesController extends BaseController<FilesService, Files> {

    @Autowired
    public FilesService service;

    /**
     * 保存
     *
     * @param dict
     * @return ResultBean<Boolean>
     */
    @PostMapping
    @RequiresPermissions("system:files:add" )
    public ResultBean<Boolean> save(Files files) {
        return ResultBean.ok(service.save(files));
    }


    /**
     * 上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResultBean<Files> upload(@RequestParam("file") MultipartFile file) {
        Files files = service.upload(file);
        if (Objects.nonNull(files)) {
            return ResultBean.ok(files, "上传成功!");
        } else {
            return ResultBean.error(ErrorCode.UPLOAD_ERROR);
        }
    }


    /**
     * 修改
     *
     * @param dict
     * @return ResultBean<Boolean>
     */
    @PutMapping
    @RequiresPermissions("system:files:edit" )
    public ResultBean<Boolean> update(Files files) {
        return ResultBean.ok(service.updateById(files));
    }

    /**
     * 删除
     *
     * @param id
     * @return ResultBean<Boolean>
     */
    @DeleteMapping("/remove/{id}" )
    @RequiresPermissions("system:files:remove" )
    public ResultBean<Boolean> remove(@PathVariable("id" ) Serializable id) {
        return ResultBean.ok(service.removeById(id));
    }

}
